package com.moli.iov.util;

import java.math.BigInteger;
import java.util.concurrent.ConcurrentHashMap;

/**
 * byte转16进制
 * Description:
 *
 * @author zql
 * @date 2017年3月27日
 */
public class ByteUtils {

    /**
     * byte 转 16进制
     *
     * @param b
     * @return
     */
    public static String toHexString(byte b) {
        int c = b & 0x000000ff;
        String cc = Integer.toHexString(c);
        cc = StringUtil.addZeroForNum(cc, 2);
        return cc;
    }

    /* Convert byte[] to hex string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)来转换成16进制字符串。
     * @param src byte[] data
     * @return  hex string
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * byte[]转int
     *
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (byte b : bytes) {
            sb.append(ByteUtils.toHexString(b));
        }
        //协议长度
        BigInteger blen = new BigInteger(sb.toString(), 16);
        int len = Integer.parseInt(blen.toString());
        return len;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }


    public static int getInt(byte[] bb, int index) {
        return (int) ((((bb[index + 0] & 0xff) << 24)
                | ((bb[index + 1] & 0xff) << 16)
                | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
    }


    public static long diffTime(Long packetNo, ConcurrentHashMap<Long, Long> p1, ConcurrentHashMap<Long, Long> p2) {
        Long t1 = p1.get(packetNo);
        Long t2 = p2.get(packetNo);
        if (t1 == null || t2 == null) {
            return 99999999999L;
        }
        return t2 - t1;
    }

    /**
     * 通过byte数组取到long
     *
     * @param bb
     * @param index
     * @return
     */
    public static long getLong(byte[] bb, int index) {

        return ((((long) bb[index + 0] & 0xff) << 56)
                | (((long) bb[index + 1] & 0xff) << 48)
                | (((long) bb[index + 2] & 0xff) << 40)
                | (((long) bb[index + 3] & 0xff) << 32)
                | (((long) bb[index + 4] & 0xff) << 24)
                | (((long) bb[index + 5] & 0xff) << 16)
                | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
    }

}
